/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var addFolder = function(btn) {
	this.btn = btn;
	this.url = "";
	this.name = "";
	this.other = "";
	this.add = function() {
		this.name = $("foldername").value;
		this.other = $("folderother").value;
		if (this.name == "") {
			this.setError("文件夹名字不能为空！");
			return;
		}
		this.url = "addfolder.jsp?name=" + encodeURI(this.name) + "&other="
				+ encodeURI(this.other) + "&time=" + new Date().getTime();
		this.btn.disabled = true;
		_ajax(this);
	};
	this.runOK = function(text) {
		if (text.replace(/\s/g, "") == "OK") {
			winClose();
			var li = document.createElement("li");
			li.innerHTML = "<a href=\"#\" onclick=\"ajax_listMail(this,1)\">"
					+ this.name + "</a>";
			$("myfolder").getElementsByTagName("ul")[0].appendChild(li);
		} else {
			this.setError(text);
			this.btn.disabled = false;
		}
	};
	this.setError = function(str) {
		var obj = this.getMessage();
		obj.className = "error";
		obj.innerHTML = str;
	};
	this.getMessage = function() {
		return $("addfolder").getElementsByTagName("p")[0];
	};
	this.setMessage = function(str) {
		var obj = this.getMessage();
		obj.className = "message";
		obj.innerHTML = str;
	}
}

var addMailbox = function(btn) {
	this.btn = btn;
	this.str = "处理中.";
	this.i = 1;
	this.url = "";
	this.address = "";
	this.password = "";
	this.add = function() {
		this.address = $("mailbox_address").value;
		this.password = $("mailbox_password").value;
		if (!this.check())
			return;
		this.url = "addmailbox.jsp?address=" + this.address + "&password="
				+ this.password + "&time=" + new Date().getTime();
		this.btn.disabled = true;
		_ajax(this);
		this.running
	};
	this.running = function() {
		this.str += "..";
		++this.i;
		if (this.i > 4) {
			this.i = 1;
			this.str = "处理中.";
		}
		this.setMessage(this.str);
	};
	this.runOK = function(text) {
		if (text.replace(/\s/g, "") == "OK") {
			$("addmailbox").innerHTML = "<p>添加成功</p><div style=\"margin:-15px 15px "
					+ "20px 15px;font-size:13px;color:#777\" align=\"left\">"
					+ "设置成功，MyMail将会对你的邮箱自动收取，发现新邮件MyMail立即提醒，"
					+ "并可直接使用该邮箱帐号发信。 请确认您的其他邮箱已经开通了POP功能，"
					+ "部分邮箱帐户（如QQ邮箱、Gmail邮箱等）可能需手工开启。</div>";
			var li = document.createElement("li");
			li.innerHTML = "<a href=\"#\">" + this.address + "</a>";
			$("maillist").getElementsByTagName("ul")[0].appendChild(li);
		} else {
			this.btn.disabled = false;
			this.setMessage(text);
		}
	};
	this.check = function() {
		if (!isEmail(this.address)) {
			this.setError("邮箱地址错误");
			return false;
		}
		if (this.password == "") {
			this.setError("密码不能为空");
			return false;
		}
		this.setError("&nbsp;");
		return true;
	};
	this.setError = function(str) {
		var obj = this.getMessage();
		obj.className = "error";
		obj.innerHTML = str;
	};
	this.getMessage = function() {
		return $("addmailbox").getElementsByTagName("p")[0];
	};
	this.setMessage = function(str) {
		var obj = this.getMessage();
		obj.className = "message";
		obj.innerHTML = str;
	}
}

var select = null;
function changeSelect(obj) {
	//alert(obj);
	if (select != obj) {
		ajax_listMail(obj, 1);
	}
	if (select != null) {
		select.className = null;
	}
	obj.className = "selected";
	select = obj;
}

var deleteMail = function() {
	this.str = "";
	this.url = "";
	this.del1 = function() {
		this.toStr();
		this.url = "deletemail.jsp?str=" + this.str + "&action=1&time="
				+ new Date().getTime();
		_ajax(this);
	};
	this.del2 = function() {
		this.toStr();
		this.url = "deletemail.jsp?str=" + this.str + "&action=2&time="
				+ new Date().getTime();
		_ajax(this);
	};
	this.del3 = function() {
		this.str = $("__mail").getAttribute("index");
		this.url = "deletemail.jsp?str=" + this.str + "&action=1&time="
				+ new Date().getTime();
		_ajax(this);
	};
	this.del4 = function() {
		this.str = $("__mail").getAttribute("index");
		this.url = "deletemail.jsp?str=" + this.str + "&action=2&time="
				+ new Date().getTime();
		_ajax(this);
	};
	this.toStr = function() {
		var select = document.getElementsByName("select");
		for (i = 0; i < select.length; ++i) {
			if (select[i].checked) {
				this.str += select[i].value + ";";
			}
		}
		this.str = this.str.substr(0, this.str.length - 1);
	};
	this.runOK = function(text) {
		addMain(text);
	};
}

function sendmail() {
	var from = document.getElementById("sendmailbox").value;
	var to = encodeURI(document.getElementById("shoujianren").value);
	var subject = encodeURI(document.getElementById("zhuti").value);
	var con = FCKeditorAPI.GetInstance('mailContent');
	//var content=con.EditorDocument.body.innerHTML;
	var content = encodeURI(con.GetXHTML(true).replace(/&/g, "\\1"));
	//content = con.GetXHTML(true).replace(/&/g, "\\1");
	//alert(content);
	//alert(subject+ content);
	if (sendmailcheck(to, subject, content)) {
		var param = "from=" + from + "&to=" + to + "&subject=" + subject
				+ "&content=" + content;
		ajaxpost("sendmail.jsp", param, sendOK, sending);
	}
}

function sendmailcheck(to, subject, content) {
	if (to == "") {
		alert("收件人不能为空！");
		return false;
	}
	if (!isEmail(to)) {
		alert("收件人不是正确的邮箱地址");
		return false;
	}
	if (subject == "") {
		alert("主题不能为空！");
		return false;
	}
	if (content == "") {
		alert("内容不能为空！");
		return false;
	}
	return true;
}
function sendOK(text) {
	alert(text);
}
function sending() {
	//alert("weihua");
}
function writemail() {
	if (select != null) {
		select.className = null;
		select = null;
	}
	var url = "writemail.jsp?time=" + new Date().getTime();
	ajax(url, "get", "addMain", "doNothing()");
}
var pre;
function returnTo() {
	document.getElementById("main").innerHTML = pre;
}
function showmail(obj) {
	var url = "showmail.jsp?id=" + obj.parentNode.id + "&time="
			+ new Date().getTime();
	ajax(url, "get", "addMain", "doNothing()");
}
function liston(obj) {
	//alert("weihua");
	obj.style.background = "#e0f0e0";
	obj.style.cursor = "pointer";
}
function listout(obj) {
	obj.style.background = "#f0f0f0";
}
function ajax_nextpage() {
	ajax("listmail.jsp?foldername=a&page=-1&id=" + new Date().getTime(), "get",
			"addMain", "doNothing();");
}
function ajax_prepage() {
	ajax("listmail.jsp?foldername=a&page=-2&id=" + new Date().getTime(), "get",
			"addMain", "doNothing();");
}
function ajax_listMail(obj, page) {
	//alert(name);
	var foldername = encodeURI(obj.innerHTML.replace(/\s/g, ""));
	//alert(foldername);
	ajax("listmail.jsp?foldername=" + foldername + "&page=" + page + "&id="
			+ new Date().getTime(), "get", "addMain", "doNothing();");
}
function addMain(text) {
	//alert(text);
	pre = document.getElementById("main").innerHTML;
	document.getElementById("main").innerHTML = text;
}
function loadRegister() {
	ajax("register.jsp", "get", "loadRegisterOK", "loadingRegister();");
}
function loadRegisterOK(text) {
	showWindow('用户注册', text);
}
function loadingRegister() {
	//alter("用户注册中");
}
function register(obj) {
	if (!checkRegister())
		return;
	//alert("weihua");
	obj.disabled = true;
	var content = document.getElementById("winContent");
	var inputs = content.getElementsByTagName("input");
	var error = content.getElementsByTagName("span")[0];
	var code = ajax_checkcode.code;
	error.style.color = "#c00";
	var username = inputs[0].value;
	var password = inputs[1].value;
	var passagain = inputs[2].value;
	var email = inputs[3].value;
	var url = "username=" + username + "&password=" + password + "&email="
			+ email + "&code=" + code + "&id=" + new Date().getTime();
	ajax("registerCheck.jsp?" + url, "get", "registerOK", "registering();");
}
function checkRegister() {
	var content = document.getElementById("winContent");
	var inputs = content.getElementsByTagName("input");
	var error = content.getElementsByTagName("span")[0];
	var btn = content.getElementsByTagName("button")[0];
	error.style.color = "#c00";
	var username = inputs[0];
	var password = inputs[1];
	var passagain = inputs[2];
	var email = inputs[3];
	if (username.value == "") {
		error.innerHTML = "用户名不能为空";
		return false;
	}
	if (password.value == "") {
		error.innerHTML = "密码不能为空";
		return false;
	}
	if (password.value.length < 6) {
		error.innerHTML = "密码不安全,请重新填写";
		//password.value="";
		return false;
	}
	//alert(password.value+" : "+passagain.value)
	if (password.value != passagain.value) {
		error.innerHTML = "两次密码不一致,请重新填写";
		//passagain.value="";
		//password.value="";
		return false;
	}
	if (!isEmail(email.value)) {
		error.innerHTML = "邮件格式错误!";
		return false;
	}
	if (!ajax_checkcode.ischecked) {
		error.innerHTML = "验证码错误!";
		return false;
	}
	error.innerHTML = "&nbsp;";
	return true;
}
var ajax_checkcode = {
	ischecked : false,
	url : "",
	code : "",
	check : function() {
		this.code = $("code").value.trim();
		this.url = "checkcode.jsp?code=" + this.code + "&time="
				+ new Date().getTime();
		_ajax(this);
	},
	runOK : function(text) {
		text = text.replace(/\s/g, "");
		if (text == "OK") {
			$("codeerror").innerHTML = "验证码正确";
			$("codeerror").style.color = "#666";
			ischeck = true;
		} else {
			$("codeerror").innerHTML = "验证码错误";
			$("codeerror").style.color = "#f00";
			$("codeimage").src = "image.jsp?time=" + new Date().getTime();
		}
	}
}

function changecode() {
	$("codeimage").src = "image.jsp?time=" + new Date().getTime();
}

function checkCode() {
	var code = document.getElementById("code").value;
	//alert(code);
	ajax("checkcode.jsp?code=" + code + "&time=" + new Date().getTime(), "GET",
			"checkcode.checkOK", "doNothing();");
}
function checkCodeOK(text) {
	text = text.replace(/\s/g, "");
	//alert(text);
	if (text == "OK") {
		$("codeerror").innerHTML = "验证码正确";
		return true;
	} else {
		$("codeerror").innerHTML = "验证码错误";
		$("codeimage").src = "image.jsp?time=" + new Date().getTime();
		return false;
	}
}

function registerOK(text) {
	//alert(text);
	text = text.replace(/\s/g, "");
	if (text == "OK") {
		winClose();
		showWindow(
				'注册成功',
				'<div align="center"><h2>恭喜你,注册成功!</h2><p><a href="/MyMail">你已成功注册,现在就去登录??</a></p></div>');
	} else {
		var content = document.getElementById("winContent");
		var str = content.getElementsByTagName("span")[0];
		str.style.color = "#c00";
		var but = content.getElementsByTagName("button")[0];
		but.disabled = false;
		str.innerHTML = text;
	}
}
function registering() {
	//alert("OK");
	var content = document.getElementById("winContent");
	var str = content.getElementsByTagName("span")[0];
	str.style.color = "#333";
	str.innerHTML = "处理中,请稍后......";
}

function logout() {
	ajax("logout.jsp?" + new Date().getTime(), "get", "logoutOK",
			"doNothing();");
}
function logoutOK(text) {
	location.reload(true);
}

function login(obj) {
	var content = document.getElementById("winContent");
	var inputs = content.getElementsByTagName("input");
	var error = content.getElementsByTagName("span")[0];
	error.style.color = "#c00";
	var username = inputs[0].value;
	var password = inputs[1].value;
	if (username == "") {
		error.innerHTML = "用户名不能为空!";
		return;
	}
	if (password == "") {
		error.innerHTML = "密码不能为空!";
		return;
	}
	//alert(username);
	obj.disabled = true;
	ajax("loginCheck.jsp?username=" + username + "&password=" + password
			+ "&id=" + new Date().getTime(), "get", "loginOK", "logining()");

}
function loginOK(text) {
	text = text.replace(/\s/g, "");
	if (text == "OK") {
		location.reload(true);
	} else {
		var content = document.getElementById("winContent");
		var error = content.getElementsByTagName("span")[0];
		var but = content.getElementsByTagName("input")[2];
		but.disabled = false;
		error.style.color = "#c00";
		error.innerHTML = text;

	}
}
function logining() {
	var content = document.getElementById("winContent");
	var str = content.getElementsByTagName("span")[0];
	str.style.color = "#333";
	str.innerHTML = "登录进行中......";
}

var xmlHttp;

function createHttp() {
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("您的浏览器不支持AJAX！");
				return false;
			}
		}
	}
}
function ajaxpost(url, param, callback, posting) {
	createHttp();
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlHttp.send(param);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200 || xmlHttp.status == 0) {
				callback(xmlHttp.responseText);
			} else {
				if (posting != null)
					posting();
			}
		} else {
			if (posting != null)
				posting();
		}
	}
}
function ajax(url, method, callback, fun2) {
	//alert("weihua1");
	createHttp();
	xmlHttp.onreadystatechange = function() {
		var text;
		if (xmlHttp.readyState == 4) {
			//alert(xmlHttp.status);
			if (xmlHttp.status == 200 || xmlHttp.status == 0) {
				//alert(callback+"(\'"+text+"\')");
				text = xmlHttp.responseText.replace(/\n/g, "");
				text = text.replace(/\r/g, "");
				//alert(callback+"(\'"+text+"\')");
				eval(callback + "(\'" + text + "\')");
			} else {
				eval(fun2);
			}
		} else {
			eval(fun2);
		}
	};
	xmlHttp.open(method, url, true);
	xmlHttp.send(null);
	//alert("weihua1");
}

function _ajax(ajax_object) {
	createHttp();
	xmlHttp.onreadystatechange = function() {
		var text;
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200 || xmlHttp.status == 0) {
				text = xmlHttp.responseText.replace(/\n/g, "").replace(/\r/g,
						"");
				ajax_object.runOK(text);
			} else {
				if (ajax_object.running != null)
					ajax_object.running();
			}
		} else {
			if (ajax_object.running != null)
				ajax_object.running();
		}
	};
	xmlHttp.open("GET", ajax_object.url, true);
	xmlHttp.send(null);

}

function isEmail(str) {
	var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
	return reg.test(str);
}

function doNothing() {

}
var left_a = {
	processed : false,
	mouseOver : function(obj) {
		obj.getElementsByTagName("p")[0].style.color = "#666";
	},
	mouseOut : function(obj) {
		obj.getElementsByTagName("p")[0].style.color = "#ddd";
	},
	in_action : function(action) {
		//alert("test");
	action();
	this.processed = true;
},
out_action : function(action) {
	if (this.processed) {
		this.processed = false;
	} else {
		action();
	}
}
}

function test() {
	alert("test");
}
function test1() {
	alert("test1");
}

function UnderLine(obj) {
	obj.className = "underline";
}
function UnderLine1(obj) {
	obj.className = null;
}
function $(id) {
	return document.getElementById(id);
}

function getError(obj) {
	return obj.getElementsByTagName("p")[0];
}

function $tag(name) {
	return document.getElementsByTagName(name);
}
